<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/1/25
 * Time: 3:52 PM
 */

namespace api\models;


class VipAssignment extends \common\models\VipAssignment
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['item_name', 'user_id'], 'required'],
            [['availability_num', 'created_at'], 'integer'],
            [['expired_time',], 'string'],
            [['item_name', 'user_id'], 'string', 'max' => 64],
            [['item_name', 'user_id'], 'unique', 'targetAttribute' => ['item_name', 'user_id']],
            [
                ['item_name'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => VipItem::className(),
                'targetAttribute' => ['item_name' => 'name']
            ],
        ];
    }


    public function beforeSave($insert)
    {

        if ( ! is_int($this->expired_time)) {
            $this->expired_time = strtotime($this->expired_time);
        }

        return parent::beforeSave($insert);
    }

    public function afterFind()
    {
        $this->expired_time = date('Y-m-d', $this->expired_time);
        parent::afterFind();
    }


}